package org.microdream.easy.deploy.service;

import ice.server.comm.BaseServiceImpl;
import ice.server.comm.CommRepository;
import ice.tool.PageVO;
import ice.tool.ResultMap;
import org.microdream.easy.deploy.dao.DGroupRepository;
import org.microdream.easy.deploy.po.DGroup;
import org.microdream.easy.deploy.utils.DateUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DGroupService extends BaseServiceImpl<DGroup, Integer> {

    @Resource
    private DGroupRepository dGroupRepository;

    @Override
    public CommRepository<DGroup, Integer> getRepository() {

        return dGroupRepository;
    }

    @Override
    public Integer getIdenty(DGroup t) {
        return t.getId();
    }

    public Map searchByPage(Map gson){
        Map resultMap = new HashMap();
        PageVO<DGroup> pv = new PageVO<>();
        Page<DGroup> p = this.findPage(gson);
        List<DGroup> dGroups = p.getContent();
        if(!CollectionUtils.isEmpty(dGroups)){
            dGroups.forEach(dGroup -> {
                dGroup.set("showUpdateTime", DateUtil.TimestampToString_1(dGroup.getUpdateTime()));
            });
        }
        resultMap.put("total", p.getTotalElements());
        resultMap.put("rows", dGroups);
        resultMap.put("page",p.getNumber());
        return resultMap;
    }

    public Map add(DGroup dGroup){
        Integer flag = this.save(dGroup);
        return ResultMap.build().add(flag>0).get();    }

    public Map upd(DGroup dGroup){
        Integer flag = this.save(dGroup);
        return ResultMap.build().update(flag>0).get();    }

    public Map del(List<Integer> ids){
        return ResultMap.build().delete(this.delete(ids)).get();
    }
}
